package com.onenine.app.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Html;
import android.util.ArrayMap;
import android.widget.TextView;

import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.onenine.app.R;
import com.onenine.app.model.ConsumeDetailItem;
import com.onenine.app.utils.DataType;
import com.onenine.common.util.ApUtils;
import com.onenine.glide.GlideApp;


public class ConsumeAdapter extends BaseQuickAdapter<ConsumeDetailItem, BaseViewHolder> {

    private final int CONSUME_TYPE = 1;
    private Context mContext;
    private int imgSize;
    private ArrayMap<String, Drawable> imgMap = new ArrayMap<>();

    public ConsumeAdapter() {
        super(R.layout.layout_consume_item);
        mContext = ApUtils.getApp();
        imgSize = mContext.getResources().getDimensionPixelSize(R.dimen.dp18);
    }

    @Override
    protected void convert(final BaseViewHolder holder, final ConsumeDetailItem item) {
        if (item.getOrderType() == DataType.ORDER_RECHARGE_TYPE) {
            holder.setText(R.id.tv_diamond_value, "+" + item.getDiamondNum());
            holder.setText(R.id.tv_diamond, R.string.diamond);
        } else {
            if (item.getConsumeType() == CONSUME_TYPE) {
                holder.setText(R.id.tv_diamond_value, "-" + item.getDiamondNum());
                holder.setText(R.id.tv_diamond, R.string.diamond);
            } else {
                holder.setText(R.id.tv_diamond_value, "+" + item.getDiamondNum());
                holder.setText(R.id.tv_diamond, R.string.coin);
            }
        }
        TextView tvDesc = holder.getView(R.id.tv_consume_content);
        holder.setText(R.id.tv_date, item.getCreateTime());
        Html.ImageGetter imgGetter = new MyImgGetter(tvDesc, item.getDescriptionContent());
        setImgGetter(tvDesc, item.getDescriptionContent(), imgGetter);
    }

    private void setImgGetter(TextView tvDesc, String desc, Html.ImageGetter imgGetter) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            tvDesc.setText(Html.fromHtml(desc, Html.FROM_HTML_MODE_COMPACT, imgGetter, null));
        } else {
            tvDesc.setText(Html.fromHtml(desc, imgGetter, null));
        }
    }

    private class MyImgGetter implements Html.ImageGetter {

        private TextView tvText;
        private String content;

        public MyImgGetter(TextView tvText, String content) {
            this.tvText = tvText;
            this.content = content;
        }

        @Override
        public Drawable getDrawable(String source) {
            Drawable drawable = imgMap.get(source);
            if (drawable != null) {
                drawable.setBounds(0, 0, imgSize, imgSize);
            } else {
                GlideApp.with(mContext).load(source).skipMemoryCache(true).into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        imgMap.put(source, resource);
                        setImgGetter(tvText, content, MyImgGetter.this);
                    }
                });
            }
            return drawable;
        }
    }

}
